﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RMS.Common;
using RMS.DB;

namespace RMS
{
    public partial class DialogCategory : ICommonForm
    {
        ConfigurationMng configurationMng = new ConfigurationMng();
        private int id;
        public DialogCategory()
        {
            InitializeComponent();
            //Set Top Most
            this.TopMost = Constants.FullScreen();
        }

        public string result
        {
            get
            {
                return txtName.Text.Trim();
            }
        }
        private void BindDataGrid()
        {
            dataGridView1.DataSource = configurationMng.GetAllItems(Constants.CategoryType);
            dataGridView1.ClearSelection();
            txtName.Focus();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (result == "")
            {
                MessageBox.Show("Hãy chọn một giá trị!", "Chú ý");
                return;
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void DialogCategory_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            BindDataGrid();
        }

        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Selected)
                {
                    txtName.Text = Convert.ToString(row.Cells[0].Value);
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                MessageBox.Show("Bạn phải nhập tên nhóm sản phẩm.", "Chú ý");
                return;
            }
            configurationMng.AddItem(Constants.CategoryType, txtName.Text.Trim());
            BindDataGrid();
            SelectAddedRow(txtName.Text.Trim());
        }

        private void SelectAddedRow(string name)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (name == Convert.ToString(row.Cells[0].Value))
                {
                    row.Cells[0].Selected = true;
                    row.Selected = true;
                    return;
                }
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            var id = "";
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Selected)
                {
                    id = Convert.ToString(row.Cells[1].Value);
                }
            }
            if (string.IsNullOrEmpty(id))
            {
                MessageBox.Show(Constants.Msg.ChooseOneRow, "Chú ý");
                return;
            }
            if (configurationMng.UpdateItem(Convert.ToInt32(id), txtName.Text.Trim()))
            {
                BindDataGrid();
                MessageBox.Show("Cập nhật thành công!", "Chú ý");
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            var id = "";
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Selected)
                {
                    id = Convert.ToString(row.Cells[1].Value);
                }
            }
            if (string.IsNullOrEmpty(id))
            {
                MessageBox.Show(Constants.Msg.ChooseOneValue, "Chú ý");
                return;
            }
            if (MessageBox.Show(Constants.Msg.ConfirmDelete, "Chú ý", MessageBoxButtons.YesNo) == DialogResult.Yes)
                if (configurationMng.DeleteItem(Convert.ToInt32(id)))
                {
                    MessageBox.Show(Constants.Msg.DeleteSuccessful, "Chú ý");
                    BindDataGrid();
                    txtName.Text = "";
                }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells[0].Selected)
                {
                    txtName.Text = Convert.ToString(row.Cells[0].Value);
                    row.Selected = true;
                    return;
                }
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            dataGridView1_CellClick(null, null);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
